package com.wfs.config;

import cn.itcast.pojo.Country;
import cn.itcast.pojo.Province;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 配置类
 * 使用配置类注册Bean对象
 */
@Configuration
public class CommonConfig {
    /**
     * 配置bean
     * @return
     */
    @Bean
    public Country country(){
        return new Country();
    }
    /**
     * 1 Bean对象的名字默认是方法名
     * 2 @Bean("aa")自定义对象名
     * 3 方法注入：Spring会自动将容器中的 Bean 注入到方法的参数中。
     * 4 使用@ConditionalOnProperty 条件注入:配置文件中前缀是province,属性名为name的值若是wfs，则声明此Bean
     * 5 @ConditionalOnMissingBean 当不存在当前类型的bean时，才声明该bean
     * 6 @ConditionalOnClass 当classpath下存在指定类时，才声明该bean
     * @return
     */
    @Bean("aa")
    @ConditionalOnProperty(prefix = "province",name = "name" ,havingValue = "wfs")
    @ConditionalOnMissingBean
    @ConditionalOnClass(name = "com.wfs.config.CommonConfig")
    public Province province(@Value("${province.name}") String name,
                             @Value("${province.direction}") String direction) {
        Province province = new Province();
        province.setName(name);
        province.setDirection(direction);
        return province;
    }
}
